package com.jh.user.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.base.MoreObjects;
import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;


@Entity
@Table(name="t_user_account")
public class UserAccount extends AbstractPersistable<Long> {

	private static final long serialVersionUID = 1L;

	/***用户ID***/
	@Column(name="user_id")
	private long userId;
	
	/***用户积分***/
	@Column(name="coin")
	private int coin = 0;
	
	/***用户钱包余额****/
	@Column(name="balance")
	private BigDecimal balance = new BigDecimal("0.00");
	
	/***用户钱包冻结金额***/
	@Column(name="freeze_balance")
	private BigDecimal freezeBalance;
	
	/**分润余额*/
	@Column(name="rebate_balance")
	private BigDecimal rebateBalance = new BigDecimal("0.00");
	
	/**被冻结的分润*/
	@Column(name="freeze_rebate_balance")
	private BigDecimal freezerebateBalance;

	/**累计分润金额*/
	@Column(name="sum_rebate_balance")
	private BigDecimal sumRebateBalance = new BigDecimal("0.00");

	/** 乐观锁记录版本号。 */
	@Column(name = "version")
	@Version
	private Integer version;

	@Column(name="create_time")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	/**分润：今日收益*/
	@Transient
	private BigDecimal todayInCome = new BigDecimal("0.00");

	@Override
	public void setId(Long id) {
        super.setId(id);
    }

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public int getCoin() {
		return coin;
	}

	public void setCoin(int coin) {
		this.coin = coin;
	}

	public BigDecimal getBalance() {
		return balance;
	}

	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public BigDecimal getRebateBalance() {
		return rebateBalance;
	}

	public void setRebateBalance(BigDecimal rebateBalance) {
		this.rebateBalance = rebateBalance;
	}

	public BigDecimal getFreezeBalance() {
		return freezeBalance;
	}

	public void setFreezeBalance(BigDecimal freezeBalance) {
		this.freezeBalance = freezeBalance;
	}

	public BigDecimal getFreezerebateBalance() {
		return freezerebateBalance;
	}

	public void setFreezerebateBalance(BigDecimal freezerebateBalance) {
		this.freezerebateBalance = freezerebateBalance;
	}

	public BigDecimal getTodayInCome() {
		return todayInCome;
	}

	public void setTodayInCome(BigDecimal todayInCome) {
		this.todayInCome = todayInCome;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

    public BigDecimal getSumRebateBalance() {
        return sumRebateBalance;
    }

    public void setSumRebateBalance(BigDecimal sumRebateBalance) {
        this.sumRebateBalance = sumRebateBalance;
    }

    /**
     * 添加用户的分润至账户余额。
     *
     * @param profit 添加的分润。
     */
    public void addProfit(BigDecimal profit) {
        if (balance == null) {
			balance = BigDecimal.ZERO;
        }
        BigDecimal value = profit.abs();
		balance = balance.add(value);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("id", getId())
                .add("userId", userId)
                .add("coin", coin)
                .add("balance", balance)
                .add("freezeBalance", freezeBalance)
                .add("rebateBalance", rebateBalance)
                .add("freezerebateBalance", freezerebateBalance)
                .add("sumRebateBalance", sumRebateBalance)
                .add("version", version)
                .add("createTime", createTime)
                .add("todayInCome", todayInCome)
                .toString();
    }

    public static UserAccount initUserAccount(long userId) {
    	UserAccount ua = new UserAccount();
    	ua.setUserId(userId);
		ua.setCoin(0);
		ua.setBalance(new BigDecimal("0.00"));
		ua.setFreezeBalance(new BigDecimal("0.00"));
		ua.setRebateBalance(new BigDecimal("0.00"));
		ua.setFreezerebateBalance(new BigDecimal("0.00"));
		ua.setSumRebateBalance(new BigDecimal(0.00));
		ua.setVersion(1);
		ua.setCreateTime(Calendar.getInstance().getTime());
		ua.setTodayInCome(new BigDecimal("0.00"));
		return ua;
	}


}
